import { Injectable } from '@angular/core';
import {
  Http,
  Response,
  URLSearchParams,
  Headers,
  RequestOptions,
  RequestOptionsArgs
} from '@angular/http';

import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

import { environment } from '../../../environments/environment';

@Injectable()
export class LabelManageService {
    private apiBaseUrl = environment.apiBaseUrl;

    constructor (private http: Http) {}

    // http://47.100.161.202:8080/admin/channel/list
    // 获取标签列表
    getLabelList(param: object): Observable<object> {
        const params: URLSearchParams = new URLSearchParams();
        params.set('name', param['labelName']);
        params.set('pageSize', param['pageSize']);
        params.set('pageNumber', param['pageNumber']);
        return this.http.get(this.apiBaseUrl + 'admin/tag/list', { params: params })
                .map(this.extractData)
                .catch(this.handleError);
    }

    // 获取全部标签列表
    getAllLabelList(): Observable<object> {
        return this.http.get(this.apiBaseUrl + 'admin/tag/getAllTag')
                .map(this.extractData)
                .catch(this.handleError);
    }

    // 新增标签接口
    postCreateLabel(param: object): Observable<object> {
        const data = new URLSearchParams();
        data.append('name', param['name']);
        param['attributeList'].forEach((label, index) => {
            data.append(`attributeList[${index}].attributeValue`, label['attributeValue']);
        });
        return this.http.post(this.apiBaseUrl + 'admin/tag/save', data)
            .map(res => res.text() ? res.json() : {})
            .catch(this.handleError);
    }
    // 修改标签接口
    postUpdateLabel(param: object): Observable<object> {
        const data = new URLSearchParams();
        data.append('id', param['updateId']);
        data.append('name', param['name']);
        data.append('order', param['order']);
        param['attributeList'].forEach((label, index) => {
            data.append(`attributeList[${index}].attributeValue`, label['attributeValue']);
            data.append(`attributeList[${index}].id`, label['attributeId'] ? label['attributeId'] : '');
        });
        return this.http.post(this.apiBaseUrl + 'admin/tag/update', data)
            .map(res => res.text() ? res.json() : {})
            .catch(this.handleError);
    }
    // 删除标签接口
    postDeleteChannel(param: object): Observable<object> {
        const data = new URLSearchParams();
        let paramUrl = '';
        param['ids'].forEach((pm, index) => {
            if ((index + 1) === param['ids'].length) {
                paramUrl += pm;
            } else {
                paramUrl += pm + ',';
            }
        });
        data.append(`ids`, paramUrl);
        return this.http.post(this.apiBaseUrl + 'admin/tag/delete', data)
            .map(res => res.text() ? res.json() : {})
            .catch(this.handleError);
    }

    private extractData(res: Response) {
        const body = res.json();
        return body || { };
    }

    private handleError (error: Response | any) {
        return Observable.throw(error);
    }
}
